package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DtsCouponServiceImpl implements DtsCouponService {

    @Autowired
    DtsCouponMapper couponMapper;

    @Override
    public IPage<DtsCoupon> findPage(Integer page, Integer limit, String name, String type, String status, String sort, String order) {
        IPage<DtsCoupon> iPage = new Page<>(page,limit);

        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted",0);

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if(!StringUtils.isEmpty(type)){
            wrapper.eq("type",type);
        }

        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }

        if(order.equalsIgnoreCase("asc")){
            wrapper.orderByAsc(sort);
        }else {
            wrapper.orderByDesc(sort);
        }

        iPage = couponMapper.selectPage(iPage,wrapper);
        return iPage;
    }

    @Override
    public DtsCoupon findAll(Integer id) {
        return couponMapper.selectById(id);
    }

    @Override
    public Object update(DtsCoupon coupon) {
        return couponMapper.updateById(coupon);
    }



    @Override
    public DtsCoupon add(DtsCoupon coupon) {
        coupon.setAddTime(new Date());
        coupon.setDeleted(false);
        couponMapper.insert(coupon);
        return coupon;
    }

    @Override
    public Object delete(DtsCoupon coupon) {
        Integer status = coupon.getStatus();
        if(status == 0){
            return ResponseUtil.fail(-1,"优惠券未过期或未下架");
        }
        coupon.setDeleted(true);
         couponMapper.updateById(coupon);
         return ResponseUtil.ok();
    }


}
